package io.finer.erp.jeecg.purchase.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.finer.erp.jeecg.purchase.entity.PurchaseLegalProvisions;
import io.finer.erp.jeecg.purchase.entity.PurchaseLegalProvisions;
import io.finer.erp.jeecg.purchase.service.PurchaseLegalProvisionsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * <p>
 * 法律条款 前端控制器
 * </p>
 *
 * @author yaoyifan
 * @since 2021-06-04
 */
@Api(tags = "采购法律条款")
@RestController
@RequestMapping("/purchase/purchaseLegalProvisions")
public class PurchaseLegalProvisionsController {
    @Autowired
    PurchaseLegalProvisionsService purchaseLegalProvisionsService;

    @AutoLog(value="法律条款-分页列表查询")
    @ApiOperation(value="法律条款-分页列表查询", notes = "法律条款-分页列表查询")
    @GetMapping("/list")
    public Result<?> queryPageList(PurchaseLegalProvisions purchaseLegalProvisions,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req){
        QueryWrapper<PurchaseLegalProvisions> queryWrapper = QueryGenerator.initQueryWrapper(purchaseLegalProvisions, req.getParameterMap());
        Page<PurchaseLegalProvisions> page = new Page<>(pageNo, pageSize);
        IPage<PurchaseLegalProvisions> pageList = purchaseLegalProvisionsService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    @AutoLog(value = "法律条款-添加")
    @ApiOperation(value="法律条款-添加", notes="法律条款-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody PurchaseLegalProvisions purchaseLegalProvisions) {
        purchaseLegalProvisionsService.save(purchaseLegalProvisions);
        return Result.ok("添加成功！");
    }

    @AutoLog(value = "法律条款-编辑")
    @ApiOperation(value="法律条款-编辑", notes="法律条款-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody PurchaseLegalProvisions purchaseLegalProvisions) {
        PurchaseLegalProvisions purchaseLegalProvisionsEntity = purchaseLegalProvisionsService.getById(purchaseLegalProvisions.getId());
        if(purchaseLegalProvisionsEntity==null) {
            return Result.error("未找到对应数据");
        }
        purchaseLegalProvisionsService.updateById(purchaseLegalProvisionsEntity);
        return Result.ok("编辑成功!");
    }

    @AutoLog(value = "法律条款-删除")
    @ApiOperation(value="法律条款-删除", notes="法律条款-删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id", required = true) String id){
        this.purchaseLegalProvisionsService.removeById(id);
        return Result.ok("删除成功!");
    }


    @AutoLog(value = "法律条款-批量删除")
    @ApiOperation(value="法律条款-批量删除", notes="法律条款-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.purchaseLegalProvisionsService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    @AutoLog(value = "法律条款-通过id查询")
    @ApiOperation(value="法律条款-通过id查询", notes="法律条款-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        PurchaseLegalProvisions purchaseLegalProvisions = purchaseLegalProvisionsService.getById(id);
        if(purchaseLegalProvisions==null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(purchaseLegalProvisions);
    }
}

